package com.plan.car.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.plan.car.entity.Post;
import com.plan.car.entity.Role;
import com.plan.car.entity.User;
import com.plan.car.entity.dto.PostDTO;
import com.plan.car.entity.dto.RoleDTO;
import com.plan.car.entity.vo.PostVO;
import com.plan.car.entity.vo.RoleVO;
import com.plan.car.service.PostService;
import com.plan.car.service.RoleService;
import com.plan.car.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/post")
@Api(tags = "岗位管理")
public class PostController {
    @Resource
    private RoleService roleService;
    @Resource
    private UserService userService;
    @Resource
    private PostService postService;

    @GetMapping("/list")
    @ApiOperation("查询岗位列表接口")
    public SaResult getPostList(){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                List<Post> postList = postService.list();
                List<PostVO> postVOList = new ArrayList<>();
                for(Post post:postList){
                    PostVO postVO = new PostVO();
                    postVO.setPostId(post.getPostId());
                    postVO.setPostName(post.getPostName());
                    postVOList.add(postVO);
                }
                return SaResult.data(postVOList);
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }
    @ApiOperation("新增岗位接口")
    @PostMapping("/add")
    public SaResult addPost(@RequestBody PostDTO postDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                Post post = new Post();
                post.setPostName(postDTO.getPostName());
                post.setCreateBy(userId);
                post.setCreateTime(new Date());
                postService.save(post);
                return SaResult.ok("新增岗位成功");
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("删除岗位接口")
    @PostMapping("/delete")
    public SaResult deletePost(@RequestBody PostDTO postDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                postService.removeById(postDTO.getPostId());
                return SaResult.ok("删除角色成功");
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("修改岗位信息接口")
    @PostMapping("/edit")
    public SaResult editPost(@RequestBody PostDTO postDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                Post post = new Post();
                post.setPostId(postDTO.getPostId());
                post.setPostName(postDTO.getPostName());
                post.setUpdateBy(userId);
                post.setUpdateTime(new Date());
                postService.updateById(post);
                return SaResult.ok("修改岗位信息成功");
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }

    @ApiOperation("查询岗位信息接口")
    @PostMapping("/select")
    public SaResult selectPost(@RequestBody PostDTO postDTO){
        if(StpUtil.isLogin()){
            long userId = StpUtil.getLoginIdAsLong();
            User loginUser = userService.getById(userId);
            Role loginRole = roleService.getById(loginUser.getRoleId());
            String roleName = loginRole.getRoleName();
            if(roleName.equals("超级管理员")){
                try{
                    Post postInfo = postService.getById(postDTO.getPostId());
                    PostVO postVO = new PostVO();
                    postVO.setPostId(postInfo.getPostId());
                    postVO.setPostName(postInfo.getPostName());
                    return SaResult.data(postVO);
                }catch (NullPointerException e){
                    return SaResult.error("岗位不存在");
                }
            } else {
                return SaResult.error("无超级管理员权限");
            }
        } else {
            return SaResult.error("当前未处于登录状态");
        }
    }
}
